package com.cspg.ssss.controller;


import com.cspg.ssss.config.WeixinProperties;
import com.cspg.ssss.service.SyncOrgUserService;
import com.cspg.ssss.service.WxUserService;
import com.cspg.ssss.vo.ResultVo;
import com.cspg.ssss.vo.ResultVoUtil;
import com.cspg.ssss.vo.UserListVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 企业微信用户管理服务接口
 */
@RestController
@RequestMapping("/wxapi")
public class WxUserController {
    private static Logger log = LoggerFactory.getLogger(WxController.class.getName());

    @Autowired
    WxUserService wxUserService;

    @Autowired
    WeixinProperties wxProperties;
    @Autowired
    SyncOrgUserService userService;

    @GetMapping("/createUser")
    public ResultVo createUser(){
        userService.syncDepartmentAndUsers();
        return ResultVoUtil.success("11", null);
    }

    /**
     * 创建用户
     *
     * @param param 新用户信息JSON对象
     * @return
     * @throws Exception
     */
    @PostMapping("/createUser")
    public ResultVo createUser(@RequestBody Map<String, String> param) throws Exception {
        userService.syncDepartmentAndUsers();
        return ResultVoUtil.success("11", null);
    }

    /**
     * 根据用户ID获取用户详细信息
     *
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping("/getUserById")
    public ResultVo getUserById(@RequestBody Map<String, String> param) throws Exception {
        return null;
    }

    /**
     * 更新用户信息
     *
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping("/updateUser")
    public ResultVo updateUser(@RequestBody Map<String, String> param) throws Exception {
        return null;
    }

    /**
     * 根据用户ID删除用户
     *
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping("/delUserById")
    public ResultVo delUserById(@RequestBody Map<String, String> param) throws Exception {
        return null;
    }

    /**
     * 根据用户列表批量删除用户
     *
     * @param param "useridlist": ["zhangsan", "lisi"]
     * @return
     * @throws Exception
     */
    @PostMapping("/batchDelUsers")
    public ResultVo batchDelUsers(@RequestBody Map<String, String> param) throws Exception {
        return null;
    }

    /**
     * 根据部门ID获取部门成员信列表
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/getUsersByDepId")
    public ResultVo getUsersByDepId(@RequestBody Map<String, String> param) {
        try {
            UserListVo vo = wxUserService.getUserListByDepartmentId(wxProperties.getCorpid(), wxProperties.getSecret(), param.get("depid"), param.get("fetchflag"));
            if (vo.getErrcode() == 0)
                return ResultVoUtil.success("success", vo);
            else
                return ResultVoUtil.error(vo.getErrcode() + "", vo.getErrmsg());
        } catch (Exception ex) {
            ResultVo<UserListVo> errVo = ResultVoUtil.error(ex.getMessage());
            return errVo;
        }
    }

    /**
     * 根据部门ID获取部门成员详细信息列表
     *
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping("/getDetailUsersByDepId")
    public ResultVo getDetailUsersByDepId(@RequestBody Map<String, String> param) throws Exception {
        return null;
    }

    /**
     * 根据用户微信OpenID获取企业微信UserID
     *
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping("/getUserIdByOpenId")
    public ResultVo getUserIdByOpenId(@RequestBody Map<String, String> param) throws Exception {
        return null;
    }

    /**
     * 根据企业微信UserID获取用户微信OpenID
     *
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping("/getOpenIdByUserId")
    public ResultVo getOpenIdByUserId(@RequestBody Map<String, String> param) throws Exception {
        return null;
    }
}
